home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Cache / Container / trifile.php < prev   
PHP Script  |  2004-10-01  |  5KB  |  147 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
  16. // |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  17. // |          Ian Eure <ieure@php.net>                                    |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: trifile.php,v 1.1 2004/04/01 07:50:07 chregu Exp $
  21.  
  22. require_once 'Cache/Container/file.php';
  23.  
  24. /**
  25.  * Tri-file cache.
  26.  *
  27.  * This cache container stores files with no special encoding to reduce overhead.
  28.  * Expiration & user data are stored in seperate files, prefixed with a '.' and
  29.  * suffixed with '.exp' & '.dat' respectively.
  30.  *
  31.  * See http://atomized.org/PEAR/Cache_trifile.html for more information.
  32.  *
  33.  * @author Ian Eure <ieure@php.net>
  34.  * @version 1.0
  35.  */
  36. class Cache_Container_trifile extends Cache_Container_file {
  37.     /**
  38.      * Fetch cached file.
  39.      *
  40.      * @param string $id Cache ID to fetch
  41.      * @param string $group Group to fetch from
  42.      * @return array 1-dimensional array in the format: expiration,data,userdata
  43.      */
  44.     function fetch($id, $group)
  45.     {
  46.         $file = $this->getFilename($id, $group);
  47.         if (!file_exists($file))
  48.             return array(NULL, NULL, NULL);
  49.         
  50.         return array(
  51.                 file_get_contents($this->_getExpFile($file)),
  52.                 file_get_contents($file),
  53.                 file_get_contents($this->_getUDFile($file))
  54.         );
  55.     }
  56.     
  57.     /**
  58.      * Get the file to store cache data in.
  59.      *
  60.      * @return string Cache data file name
  61.      * @access private
  62.      */
  63.     function _getFile($file)
  64.     {
  65.         $dir = dirname($file);
  66.         $file = basename($file);
  67.         return $dir.'/.'.$file;
  68.     }
  69.     
  70.     /**
  71.      * Get the file to store expiration data in.
  72.      *
  73.      * @return string Expiration data file name
  74.      * @access private
  75.      */
  76.     function _getExpFile($file)
  77.     {
  78.         return $this->_getFile($file).'.exp';
  79.     }
  80.     
  81.     /**
  82.      * Get the file to store user data in.
  83.      *
  84.      * @return string User data file name
  85.      * @access private
  86.      */
  87.     function _getUDFile($file)
  88.     {
  89.         return $this->_getFile($file).'.dat';
  90.     }
  91.     
  92.     /**
  93.      * Cache file
  94.      *
  95.      * @param string $id Cache ID
  96.      * @param mixed $cachedata Data to cache
  97.      * @param mixed $expires When the data expires
  98.      * @param string $group Cache group to store data in
  99.      * @param mixed $userdata Additional data to store
  100.      * @return boolean true on success, false otherwise
  101.      */
  102.     function save($id, $cachedata, $expires, $group, $userdata)
  103.     {
  104.         $this->flushPreload($id, $group);
  105.  
  106.         $file = $this->getFilename($id, $group);
  107.         if (PEAR::isError($res = $this->_saveData($file, $cachedata))) {
  108.             return $res;
  109.         }
  110.         if (PEAR::isError($res = $this->_saveData($this->_getExpFile($file), $expires))) {
  111.             return $res;
  112.         }
  113.         if(PEAR::isError($res = $this->_saveData($this->_getUDFile($file), $userData))) {
  114.             return $res;
  115.         }
  116.  
  117.         return true;
  118.     }
  119.  
  120.     /**
  121.      * Save data in a file
  122.      *
  123.      * @param string $file File to save data in
  124.      * @param string $data Data to save
  125.      * @return mixed true on success, Cache_Error otherwise
  126.      */
  127.     function _saveData($file, $data) {
  128.         // Save data
  129.         if (!($fh = @fopen($file, 'wb')))
  130.             return new Cache_Error("Can't access '$file' to store cache data. Check access rights and path.", __FILE__, __LINE__);
  131.         
  132.         if ($this->fileLocking) {
  133.             flock($fh, LOCK_EX);
  134.         }
  135.         
  136.         fwrite($fh, $data);
  137.         
  138.         if($this->fileLocking) {
  139.             flock($fh, LOCK_UN);
  140.         }
  141.         
  142.         fclose($fh);
  143.         return true;
  144.     }
  145. }
  146.  
  147. ?>